<template>
	<view class="container bg-fff">
		<up-navbar placeholder>
			<template #left>
				<view>
					<image class="logo" src="https://image.srea.org.cn/xcx/xiehui_and_market_logo.png" mode="heightFix"></image>
				</view>
			</template>
		</up-navbar>

		<banner></banner>

		<menus :policyData="brandData.zcxx_list" :huanXinData="brandData.o2n_list"></menus>

		<notice :text="noticeList" fontSize="26" v-if="noticeList.length > 0"></notice>

		<brand :detail="brandData"></brand>

		<view class="batchWrap bg-fff" v-if="batchList.length > 0">
			<view class="batchBody">
				<up-tabs :list="batchList" :scrollable="false" :current="currentTab" lineColor="#B99354" :activeStyle="activeStyle"
					:inactiveStyle="inactiveStyle" @click="clickTabs"></up-tabs>

				<view class="batch-list font13" v-if="batchList[currentTab].list.length > 0">
					<view class="batch-item flex" v-for="(item, index) in batchList[currentTab].list" :key="index">
						<text class="batch-title txtHidden col-grey" @click="clickArticleItem(item)">{{ item.title }}</text>
						<text class="status col-B99354" @click="clickMoreArticle(item)">查看更多</text>
					</view>
				</view>
				<up-empty text="暂无数据" mode="list" marginTop="100" v-if="batchList[currentTab]?.list.length === 0"></up-empty>
			</view>
		</view>

		<up-divider text="人民城市      人民建" textSize="12"></up-divider>

		<up-gap height="30"></up-gap>

		<!-- <up-modal :show="showStatisticsForm" title="数据统计采集" :showCloseBtn="false" :showConfirmButton="false">
            <statistics-form class="width-100"></statistics-form>
        </up-modal> -->
		
		<tabbar></tabbar>
	</view>
</template>

<script setup>
import Banner from './components/banner.vue';
import Menus from './components/menus.vue';
import Notice from './components/notice.vue';
import Brand from './components/brand.vue';
import { ref } from 'vue';
import { useUserInfoStore } from '@/store/index';
import { onShow, onLoad, onPullDownRefresh, onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app';
import { homeAPI } from '@/common/api/api'

const userInfoStore = useUserInfoStore();

let activeStyle = ref({
	color: '#007235',
	fontWeight: 'bold',
	fontSize: '28rpx'
});
let inactiveStyle = {
	color: '#8A8A8A',
	fontSize: '28rpx'
}

//公告列表
let noticeList = ref([]);

//品牌馆卡片信息
let brandData = ref({});

//批次
let currentTab = ref(0);
let batchList = ref([
	{
		name: '批次公示详情',
		type: 'pici',
		list: []
	},
	{
		name: '各区动态',
		type: 'dongTai',
		list: []
	},
	{
		name: '政务公开信息',
		type: 'zhengWu',
		list: []
	}
])

onShow(() => {
	//延迟1秒再验证验证是否填写上周数据
	// setTimeout(async () => {
	// 	await userInfoStore.checkLastWeekVisitData();

	// 	if(userInfoStore.showStatisticsForm) {
	// 		uni.showModal({
	// 			title: '提示',
	// 			showCancel: false,
	// 			content: '检测到您上周未提交到访数据，请及时提交',
	// 			success: (res) => {
	// 				if(res.confirm) {
	// 					uni.navigateTo({
	// 						url: `/pages/my/statistics/form/index?startDate=${userInfoStore.statisticsFormObj.start_time}&endDate=${userInfoStore.statisticsFormObj.end_time}`
	// 					})
	// 				}
	// 			}
	// 		})
	// 	}
	// }, 1500)
})

// 页面加载
onLoad((options) => {
	if (options.puid) {
		userInfoStore.setPuid(options.puid)
	}
	uni.$check_and_login(init)
})

// 下拉刷新
onPullDownRefresh(() => {
	init();
})

// 分享到好友
onShareAppMessage(() => {
	return {
		title: '新房超市',
		path: `/pages/tabBar/home/index?puid=${userInfoStore.userInfo.id}`,
		imageUrl: '',
	}
})

// 分享到朋友圈
onShareTimeline(() => {
	return {
		title: '新房超市',
		query: `puid=${userInfoStore.userInfo.id}`,
		imageUrl: '',
	}
})

//初始化
async function init(params) {
	try {
		await userInfoStore.checkUserInfo();//验证用户信息,如果没有就获取并且更新
		const { status, detail } = await homeAPI();
		if(status !== 0) {
			uni.stopPullDownRefresh();
			uni.$u.toast(detail);
			return
		}
		brandData.value = detail;

		//摇号公示
		if(detail.yao_list.length > 0) {
			noticeList.value = detail.yao_list;
		}

		//批次
		if(detail.pc_list.length > 0) {
			const findPiCi = batchList.value.find(item => item.type === 'pici');
			findPiCi.list = detail.pc_list.slice(0, 3);
		}

		//各区动态
		if(detail.area_article.length > 0) {
			const findAreaDynamic = batchList.value.find(item => item.type === 'dongTai');
			findAreaDynamic.list = detail.area_article.slice(0, 3);
		}

		//政务公开信息
		if(detail.zhengw.length > 0) {
			const findZhengWu = batchList.value.find(item => item.type === 'zhengWu');
			findZhengWu.list = detail.zhengw.slice(0, 3);
		}
		
		setTimeout(() => {
			uni.stopPullDownRefresh()
		}, 500)
	} catch (error) {}
}

//点击切换tab
function clickTabs({index}){
	currentTab.value = index;
}

//点击文章跳转
function clickArticleItem(info) {
	const currTabObj = batchList.value[currentTab.value];
	switch(currTabObj.type) {
		case 'pici':
			uni.navigateTo({
				url: `/pages/tools/pici/pages?piciId=${info.id}`
			})
			break;

		case 'dongTai':
			uni.navigateTo({
				url: `/pages/tools/area-dynamic/detail/index?detailId=${info.id}`
			})
			break;

		case 'zhengWu':
			uni.navigateTo({
				url: `/pages/tools/zheng-wu/detail/index?detailId=${info.id}`
			})
			break;

		default:
			break
	}
}

//点击查看更多
function clickMoreArticle(item) {
	const currTabObj = batchList.value[currentTab.value];
	let url = '';
	switch(currTabObj.type) {
		case 'pici':
			url = `/pages/tools/pici/pages?piciId=${item.id}`
			break;

		case 'dongTai':
			url = `/pages/tools/area-dynamic/list/index`
			break;
		case 'zhengWu':
			url = `/pages/tools/zheng-wu/list/index`
			break;
		default:
			break;
	}
	uni.navigateTo({
		url: url
	})
}
</script>

<style lang="scss" scoped>
.container {
	.logo {
		height: 40rpx;
	}
}

.batchWrap {
	padding: 20rpx;

	.batchBody {
		border-radius: 20rpx;
		background-color: #F3F3F4;

		.batch-list {
			padding: 20rpx;

			.batch-item {
				padding: 20rpx 0;
				justify-content: space-between;

				.batch-title {
					width: 80%;
				}
			}
		}
	}

}
</style>